package com.crm.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.crm.model.jdao.IOrderInfoDao;
import com.crm.model.jdao.IOrderitemsDao;
import com.crm.model.jdao.IProductDAO;
import com.crm.model.mdao.OrderitemsMapper;
import com.crm.model.mdao.ProductMapper;
import com.crm.model.pojo.MOrderItem;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Orderitem;
import com.crm.model.pojo.Product;
import com.crm.model.pojo.Returnmoney;
import com.github.pagehelper.PageHelper;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderitemsServices {
	@Autowired
	IOrderitemsDao iOrderitemsDao;
	@Autowired
	IOrderInfoDao iOrderInfoDao;
	@Autowired
	IProductDAO iProductDAO;
	
	@Autowired
	OrderitemsMapper orderitemsMapper;
	
	@Autowired
	ProductMapper productMapper;
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,Integer statusid ,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Map<String, String>> list = orderitemsMapper.selectAllOrderTable(search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 分页查询产品
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager1(int pageNum, int pageSize,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Map<String, String>> list = productMapper.productAllTable(search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 查询所有订单信息
	 * @return 
	 */
	public List<Map<String, String>> selectAllOrderItem() {
		return orderitemsMapper.selectAllOrderItem();
	}
	
	/**
	 * 查询产品ID和名字用于前台绑定和编辑订单详情商品展示
	 * @return
	 */
	public List<Map<String, String>> productAllPage() {
		return productMapper.productAllPage();
	}
	

	/**
	 * 根据订单编号查询订单详情信息 
	 * @param roid
	 * @return
	 */
	public List<Map<String, String>> custByOrderitemsInfo(Integer oid){
		return orderitemsMapper.custByOrderitemsInfo(oid);
	}
	
	/**
	 * 添加、修改回款计划
	 */
	public Orderitem addItem(Orderitem orderitem,Orderinfo orderinfo) {
		float oMaxMoney=Float.parseFloat(orderitem.getOiMoney().toString());
		System.out.println(orderinfo.getOId());
		 List<Map<String, BigDecimal>> map = orderitemsMapper.selectOrderItemMoney(orderinfo.getOId());
		 for (Map<String, BigDecimal> map2 : map) {
			 Collection<BigDecimal> values = map2.values();
				for (BigDecimal string : values) {
					oMaxMoney+=Float.parseFloat(string.toString());
				}
		}
		System.out.println("总值"+oMaxMoney);
		orderitem.getOrderinfo().setOSummoney(new BigDecimal(oMaxMoney));
		Orderitem orderitemp= iOrderitemsDao.save(orderitem);
		System.out.println(orderitemp);
		return orderitemp;
	}
	
	/**
	 * 用于前台选择产品显示价格
	 * @return
	 */
	public List<Map<String,String>>  productoiPage(Integer proid){
		return productMapper.productoiPage(proid);
	}
	
	/**
	 * 订单视图页订单详情数据展示
	 * @return
	 */
	public List<Map<String,String>>  selectOrderItemView(Integer oid){
		return orderitemsMapper.selectOrderItemView(oid);
	}
	
	/**
	 * 编辑订单视图页添加订单详情
	 *"p_unit":"台","money":4500,"p_name":"华为",
	 *"price":4500,"o_id":12,"count":1,"discount":0.9,"remark":"",
	 *"ID":21,"technology":"华为","p_id":1,"price1":5000}
	 * @return
	 */
	public void  addorderitemview(List<Map<String,String>> limap,Integer id){
		/*List<Orderitem> list =new ArrayList<>();
		Orderinfo orderinfo = iOrderInfoDao.findById(id).get();
		System.out.println(orderinfo.getOrderitems());
		float maxMoney=0; 
		for (Map<String, String> map : limap) {
			Orderitem orderitem = new Orderitem();
			Product product = iProductDAO.findById(Integer.parseInt(String.valueOf(map.get("p_id")))).get();
			//判断是否是已有订单详情
			if(String.valueOf(map.get("ID"))!="null") {
				orderitem.setOiId(Integer.parseInt(String.valueOf(map.get("ID"))));
				Orderitem orderitem2 = iOrderitemsDao.findById(Integer.parseInt(String.valueOf(map.get("ID")))).get();
				iOrderitemsDao.delete(orderitem2);
				System.out.println("来了");
			}else {
				orderitem.setOiDate(new Date());//设置时间为当前时间
			}
			orderitem.setOrderinfo(orderinfo);
			orderitem.setProduct(product);//设置产品
			orderitem.setCustId(orderinfo.getCustomer().getCustId());//设置对应的客户
			orderitem.setOiPrice(new BigDecimal(String.valueOf(map.get("price"))));//设置详情的实际交易价格
			orderitem.setOiCount(new BigDecimal(String.valueOf(map.get("count"))));//数量
			orderitem.setOiDiscount(new BigDecimal(String.valueOf(map.get("discount"))));//折扣
			orderitem.setOiMoney(new BigDecimal(String.valueOf(map.get("money"))));//金额
			orderitem.setRemark(String.valueOf(map.get("remark")));
			orderitem.setStatus(1102);//订单详情状态
			orderitem.setuId(orderinfo.getUId());//订单所对应的所有人
			System.out.println("订单详情ID"+orderitem.getOiId());
			//iOrderitemsDao.save(orderitem);
			list.add(orderitem);
			maxMoney+=Float.parseFloat(String.valueOf(map.get("money")));
		}
		System.out.println(list);
		orderinfo.setOrderitems(list);
		orderinfo.setOSummoney(new BigDecimal(maxMoney));//更改订单详情
		iOrderInfoDao.save(orderinfo);*/
		Orderinfo orderinfo = iOrderInfoDao.findById(id).get();
		Integer deleteOrderItemByid = orderitemsMapper.deleteOrderItemByid(id);
		float maxMoney=0; 
		for (Map<String, String> map : limap) {
			MOrderItem mOrderItem = new MOrderItem();
			mOrderItem.setoId(Integer.parseInt(String.valueOf(map.get("o_id"))));
			mOrderItem.setpId(Integer.parseInt(String.valueOf(map.get("p_id"))));
			mOrderItem.setOiDate(orderinfo.getOFadate());//设置时间为当前时间
			mOrderItem.setOiId(String.valueOf(map.get("ID")));
			mOrderItem.setOiDate(new Date());
			mOrderItem.setCustId(orderinfo.getCustomer().getCustId());//设置对应的客户
			mOrderItem.setOiPrice(new BigDecimal(String.valueOf(map.get("price"))));//设置详情的实际交易价格
			mOrderItem.setOiCount(new BigDecimal(String.valueOf(map.get("count"))));//数量
			mOrderItem.setOiDiscount(new BigDecimal(String.valueOf(map.get("discount"))));//折扣
			mOrderItem.setOiMoney(new BigDecimal(String.valueOf(map.get("money"))));//金额
			mOrderItem.setRemark(String.valueOf(map.get("remark")));
			mOrderItem.setStatus(1102);//订单详情状态
			mOrderItem.setuId(orderinfo.getUId());//订单所对应的所有人
			maxMoney+=Float.parseFloat(String.valueOf(map.get("money")));
			orderitemsMapper.insertOrderItem(mOrderItem);
		}
		orderinfo.setOSummoney(new BigDecimal(maxMoney));//更改订单详情
		iOrderInfoDao.save(orderinfo);
	}
}
